package com.html;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;

import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Node;

/**
 * *********************************************
 * @author Administrator
 * @FileName W3CNodeArray.java
 * @Description解析org.w3c.dom.Node[]
 **********************************************
 */
public class ParseW3CNodeArray {
	
	public String nodeToHtml(Node node){
		String result = null;
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		StreamResult ot = new StreamResult(os);
		try {
			TransformerFactory.newInstance().newTransformer().transform(new DOMSource(node), ot);
			result = os.toString("UTF-8");
			if(result != null && !"".equals(result)){
				/**
				 * org.w3c.dom.Node转html时会带上<?xml version="1.0" encoding="UTF-8"?>标签，可能跟解析XML的时候有关
				 */
				result = result.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");
			}
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		} catch (TransformerFactoryConfigurationError e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}

}
